package com.wyzz.hr.action;

import java.util.*;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IAttdoriginaldataBO;
import com.wyzz.hr.bo.IAttendmonthlyBO;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.impl.ExaminDateUtil;
import com.wyzz.hr.pojo.Attendmonthly;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.shift.AttendDailyHandler;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.support.RecordSorter;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.DateUtil;
import com.wyzz.hr.util.StringUtil;

public class DwrForAttend extends BaseAction {

	public DwrForAttend() {
		msgNoAuth = "{0}:\u60A8\u65E0\u6743\u6267\u884C\u6B64\u64CD\u4F5C\uFF0C\u8BF7\u91CD\u65B0\u767B\u9646\uFF01";
		msgNoEmp = "{0}:\u5458\u5DE5\u4E0D\u5B58\u5728\uFF01\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5\uFF01";
		msgNoAttm = "{0}:\u8003\u52E4\u8BB0\u5F55\u4E0D\u5B58\u5728\uFF01\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5\uFF01";
		msgOperSucc = "{0}:{1}\u64CD\u4F5C\u6210\u529F\uFF01";
	}

	public Map getEmpAttendmonthly(String id) {
		Map rs = new HashMap();
		if ("error".equalsIgnoreCase(DWRUtil.checkAuth("DwrForAttend",
				"getEmpAttendmonthly"))) {
			rs.put("error", StringUtil.message(msgNoAuth,
					new Object[] { "noauth" }));
			return rs;
		}
		IAttendmonthlyBO attendmothlyBO = (IAttendmonthlyBO) SpringBeanFactory
				.getBean("attendmonthlyBo");
		Attendmonthly monthly = attendmothlyBO.getAttendmonthly(id);
		if (monthly != null)
			rs.put("param_succ", monthly);
		else
			rs.put("error", StringUtil.message(msgNoAttm,
					new Object[] { "noattm" }));
		return rs;
	}

	public Map saveEmpAttendmonthly(Attendmonthly attendmonthly, String empId,
			String mode) throws Exception {
		Map rs = new HashMap();
		String auth = DWRUtil.checkAuth("DwrForAttend", "saveEmpAttendmonthly");
		if ("error".equals(auth)) {
			rs.put("error", StringUtil.message(msgNoAuth,
					new Object[] { "noauth" }));
			return rs;
		} else {
			attendmonthly.setAttmEmpId(new Employee(empId));
			IAttendmonthlyBO attendmothlyBO = (IAttendmonthlyBO) SpringBeanFactory
					.getBean("attendmonthlyBo");
			Attendmonthly data = attendmothlyBO.saveOrUpdateAttendmonthly(
					attendmonthly, mode);
			rs.put("success", StringUtil.message(msgOperSucc, new Object[] {
					"SUCC", "\u6708\u8003\u52E4\u6C47\u603B" }));
			rs.put("param_succ", data);
			return rs;
		}
	}

	public Attendmonthly recalcEmpAttendmonthly(String yearMonth, String empId) {
		if (StringUtils.isEmpty(yearMonth))
			return null;
		IAttendmonthlyBO attendmothlyBO = (IAttendmonthlyBO) getBean("attendmonthlyBo");
		List result = attendmothlyBO.exeMonthlySummary(yearMonth,
				new String[] { empId });
		if (result.isEmpty())
			return null;
		else
			return (Attendmonthly) result.get(0);
	}

	public Map getAttendDailyMemory(String id, String yearMonth) {
		Map rs = new HashMap();
		if ("error".equals(DWRUtil.checkAuth("DwrForAttend",
				"getAttendDailyMemory"))) {
			rs.put("error", StringUtil.message(msgNoAuth,
					new Object[] { "noauth" }));
			return rs;
		}
		IEmployeeBo empBo = (IEmployeeBo) SpringBeanFactory.getBean("empBo");
		Employee emp = empBo.loadEmp(id, null);
		if (emp == null) {
			rs.put("error", StringUtil.message(msgNoEmp,
					new Object[] { "noemp" }));
			return rs;
		} else {
			List empList = new ArrayList();
			empList.add(emp);
			String config = DatabaseSysConfigManager.getInstance().getProperty(
					"sys.examin.month.sum");
			java.util.Date dateArr[] = ExaminDateUtil.getDateArray(yearMonth,
					config);
			AttendDailyHandler handler = new AttendDailyHandler();
			handler.setEmpList(empList);
			handler.setStartDate(dateArr[0]);
			handler.setEndDate(dateArr[1]);
			List list = handler.generateRecords();
			RecordSorter sorter = new RecordSorter(list);
			sorter.doSort("examinDate", "asc");
			rs.put("param_succ", list);
			return rs;
		}
	}

	public String addCardData(String empIdStr, String attdDateStr,
			String attdCardTimeStr, String memo, String curentEmpName) {
		if ("error".equals(DWRUtil
				.checkAuth("originalDataImportSho", "execute")))
			return StringUtil.message(msgNoAuth, new Object[] { "noauth" });
		if (StringUtils.isEmpty(empIdStr)) {
			return StringUtil.message(msgNoEmp, new Object[] { "noemp" });
		} else {
			java.util.Date attdDate = DateUtil.parseDateByFormat(attdDateStr,
					"yyyy-MM-dd");
			java.util.Date attdCardTime = DateUtil.parseDateByFormat(
					attdCardTimeStr, "yyyy-MM-dd HH:mm:ss");
			String empNoArray[] = empIdStr.split(";");
			IAttdoriginaldataBO originaldataBo = (IAttdoriginaldataBO) SpringBeanFactory
					.getBean("attdoriginaldataBO");
			originaldataBo.saveOriginalData(empNoArray, attdDate, attdCardTime,
					null, memo, curentEmpName);
			return StringUtil.message(msgOperSucc, new Object[] { "SUCC",
					"\u8865\u5361" });
		}
	}

	private static final long serialVersionUID = 1L;
	public String msgNoAuth;
	public String msgNoEmp;
	public String msgNoAttm;
	public String msgOperSucc;
}
